import 'package:flutter/material.dart';
import './inheried_data_widget.dart';
import './person.dart';

class InheriedPage extends StatefulWidget {
  const InheriedPage({super.key});
  @override
  State<InheriedPage> createState() => _InheriedPage();
}
class _InheriedPage extends State<InheriedPage> {
  Person person =  Person('Tome', 18);
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: const Text('Inheriedwidget'),
      ),
      body: Center(
        child: InheriedDataWidget(
          person: person,
          child: const PageA1(),
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () => setState(() {
          person = Person(person.name, person.age + 1);
        }),
        tooltip: 'Increment',
        child: const Icon(Icons.add),
      ),
    );
  }
}

class PageA1 extends StatelessWidget {
  const PageA1({super.key});
  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.grey,
      width: MediaQuery.of(context).size.width,
      margin: const EdgeInsets.all(10),
      child: Column(
        children: [
          const Text(
            "A1 Widget",
            style: TextStyle(color: Colors.white,fontSize: 20),
          ),
          Text(
            "共享数据的信息: ${InheriedDataWidget.of(context)!.person.toString()}",
            style: const TextStyle(color: Colors.white,fontSize: 20),
          ),
          Container(
            margin: const EdgeInsets.only(top: 40),
            color: Colors.deepOrangeAccent,
            height: MediaQuery.of(context).size.height*0.5,
            width: MediaQuery.of(context).size.width,
            child: const Row(
              mainAxisAlignment: MainAxisAlignment.spaceAround,
              children: <Widget>[
                PageB1(),
                PageB2(),
                PageB3(),
              ],
            )
          )
        ]
      ),
    );
  }
}
///
class PageB1 extends StatefulWidget {
  const PageB1({super.key});
  @override
  _PageB1State createState() => _PageB1State();
}
class _PageB1State extends State<PageB1> {
  @override
  Widget build(BuildContext context) {
    debugPrint("---------> page 1 build");
    return Container(
      color: Colors.white,
      height: 140,
      width: MediaQuery.of(context).size.width/3-20,
      child: Text("PageB1 ful\n\n共享数据是：${InheriedDataWidget.of(context)!.person.toString()}"),
    );
  }
  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    debugPrint("PageB1 ful ===> didChangeDependencies");
  }
}
///
class PageB2 extends StatelessWidget {
  const PageB2({super.key});
  @override
  Widget build(BuildContext context) {
    debugPrint("---------> page 2 build");
    return Container(
      color: Colors.white,
      height: 140,
      width: MediaQuery.of(context).size.width/3-20,
      child: Text("PageB2\n\n共享数据是：${InheriedDataWidget.of(context)!.person.toString()}"),
    );
  }
}
///
class PageB3 extends StatefulWidget {
  const PageB3({super.key});
  @override
  _PageB3State createState() => _PageB3State();
}
class _PageB3State extends State<PageB3> {
  @override
  Widget build(BuildContext context) {
    debugPrint("---------> page 3 build");
    return Container(
      color: Colors.white,
      height: 140,
      width: MediaQuery.of(context).size.width/3-20,
      child: const Text("PageB3 ful\n\n未引用共享数据信息"),
    );
  }
  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    debugPrint("PageB3 ===> didChangeDependencies");
  }
}